﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Physis.Data.Readers {

    public class FormReader {

        public static String StringFromForm (System.Collections.Specialized.NameValueCollection form, String fieldName, String defaultValue = "") {

            String value = defaultValue;

            if (form[fieldName] != null) { value = form[fieldName]; }

            return value;

        }

        public static Byte ByteFromForm (System.Collections.Specialized.NameValueCollection form, String fieldName, Byte defaultValue = 0) {

            Byte value = defaultValue;


            if (form[fieldName] != null) { Byte.TryParse (form[fieldName], out value); }


            return value;

        }

        public static Int32 Int32FromForm (System.Collections.Specialized.NameValueCollection form, String fieldName, Int32 defaultValue = 0) {

            Int32 value = defaultValue;


            if (form[fieldName] != null) { Int32.TryParse (form[fieldName], out value); }


            return value;

        }

        public static Int64 Int64FromForm (System.Collections.Specialized.NameValueCollection form, String fieldName, Int64 defaultValue = 0) {

            Int64 value = defaultValue;


            if (form[fieldName] != null) { Int64.TryParse (form[fieldName], out value); }


            return value;

        }

        public static Decimal DecimalFromForm (System.Collections.Specialized.NameValueCollection form, String fieldName, Decimal defaultValue = 0) {

            Decimal value = defaultValue;


            if (form[fieldName] != null) { Decimal.TryParse (form[fieldName].Replace ("$", ""), out value); }


            return value;

        }

        public static Boolean BooleanFromForm (System.Collections.Specialized.NameValueCollection form, String fieldName, Boolean defaultValue = false) {

            Boolean value = defaultValue;


            if (form[fieldName] != null) {

                if (!Boolean.TryParse (form[fieldName], out value)) {

                    value = (form[fieldName].ToLower ().Contains ("true"));

                    value = value || (form[fieldName].ToLower ().Contains ("yes")); // SUPPORT YES FROM JQGRID POSTBACK OF CHECKBOXES

                    value = value || (form[fieldName].ToLower ().Contains ("on")); // SUPPORT ON FROM CHECK BOXES 

                }

            }


            return value;

        }

        public static DateTime DateTimeFromForm (System.Collections.Specialized.NameValueCollection form, String fieldName) {

            DateTime value = DateTime.Today;


            if (form[fieldName] != null) { DateTime.TryParse (form[fieldName], out value); }


            return value;

        }

        public static DateTime? DateTimeFromDatabaseAllowNull (System.Collections.Specialized.NameValueCollection form, String fieldName) {

            DateTime? value = null;

            DateTime convertedValue = DateTime.Today;


            if (form[fieldName] != null) {

                if (DateTime.TryParse (form[fieldName], out convertedValue)) {

                    value = convertedValue;

                }

            }


            return value;

        }

    }

}
